package app.util
{
	import flash.filesystem.*;
	
	public class Logger
	{
		private var enabled:Boolean = true;
		private var fs:FileStream;
		
		public function Logger()
		{
			var logName:String = File.applicationDirectory.resolvePath(Constants.LOG_FILE).nativePath;
			fs = new FileStream();			
			fs.open(new File(logName), FileMode.APPEND);
			this.logStart();
			this.log("Open logfile : " + logName);
		}

		public function setEnable(v:Boolean):void
		{
			if(!this.enabled && v)
			{
				this.logText("Logfile enabled");
			}
			else if(this.enabled && !v)
			{
				this.logText("Logfile disabled");
			}
			
			this.enabled = v;
		}
		
		public function log(text:String):void
		{
			if(this.enabled)
			{
				this.logText(text);
			}
		}
		
		private function logStart():void
		{
			this.logToFile("\n\n");
		}
		
		private function logText(text:String):void
		{
			var time:String = new Date().toLocaleString();
			this.logToFile("[" + time + "]:  " + text + "\n");
		}	
		
		private function logToFile(text:String):void
		{
			fs.writeUTFBytes(text);
		}
	}
}